local heqia = fk.CreateSkill {

  name = "joy__heqia",

  tags = {  },

}



heqia:addEffect("active", {
  name = "joy__heqia",
  min_card_num = 0,
  target_num = 1,
  prompt = "#joy__heqia-invoke",
  can_use = function(self, player)
    return player:usedSkillTimes(heqia.name, Player.HistoryPhase) == 0
  end,
  interaction = function(self,player)
    local choices = {}
    if not player:isNude() then table.insert(choices, "heqia_give") end
    if table.find(Fk:currentRoom().alive_players, function(p) return player ~= p and not p:isNude() end) then
      table.insert(choices, "heqia_prey")
    end
    return UI.ComboBox {choices = choices}
  end,
  card_filter = function(self, player, to_select, selected)
    if not self.interaction.data or self.interaction.data == "heqia_prey" then return false end
    return true
  end,
  target_filter = function(self, player, to_select, selected)
    if not self.interaction.data or #selected > 0 or to_select == player.id then return false end
    if self.interaction.data == "heqia_give" then
      return #selected_cards > 0
    else
      return not Fk:currentRoom():getPlayerById(to_select):isNude()
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local toid = effect.tos
    local cards = effect.cards
    local dummy = Fk:cloneCard("dilu")
    local to = player
    if #cards > 0 then
      to = room:getPlayerById(toid[1])
      dummy:addSubcards(cards)
    else
      local src = room:getPlayerById(toid[1])
      local cards = room:askForCard(src, 1, 999, true, heqia.name, false, ".", "#heqia-give:"..player.id)
      dummy:addSubcards(cards)
    end
    room:moveCardTo(dummy,Card.PlayerHand,to,fk.ReasonGive,heqia.name,nil,false,player)
    if to.dead or to:isKongcheng() then return end
    room:setPlayerMark(to, "heqia-tmp", #dummy.subcards)
    local success, dat = room:askForUseActiveSkill(to, "joy__heqia_viewas", "#heqia-use:::"..#dummy.subcards, true)
    if success and dat then
      local card = Fk:cloneCard(dat.interaction)
      card:addSubcards(dat.cards)
      room:useCard{
        from = to.id,
        tos = table.map(dat.targets, function(id) return {id} end),
        card = card,
        extraUse = true,
      }
    end
  end,
})

return heqia